<?php
namespace app\restaurant\model;

use app\BaseModel;
use think\facade\Db;

class SpePrice extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_restaurant_spe_price';


    protected $append = [

        'spe_name'

    ];


    /**
     * @param $value
     * @param $data
     * @功能说明:
     * @author chenniang
     * @DataTime: 2020-10-09 16:54
     */
    public function getSpeNameAttr($value,$data){

        if(!empty($data['spe_id'])){

            $pec_id   = explode('-',$data['spe_id']);

            $spe_name = Db::name('longbing_card_restaurant_spe')->where(['status'=>1])->where('id','IN',$pec_id)->column('title');

            return  implode('-',$spe_name);

        }

    }

    /**
     * @author chenniang
     * @DataTime: 2020-10-09 16:45
     * @功能说明:
     */

    public function getSpeIdAttr($value,$data){

        return explode('-',$value);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:04
     * @功能说明:添加
     */
    public function dataAdd($data){

        $res = $this->insert($data);

        return $res;

    }



    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:05
     * @功能说明:编辑
     */
    public function dataUpdate($dis,$data){

        $res = $this->where($dis)->update($data);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-10-09 14:58
     * @功能说明:
     */
    public function dataInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];

    }

    /**
     * @param $data
     * @param $good_id
     * @param $arr
     * @功能说明:
     * @author chenniang
     * @DataTime: 2020-10-09 14:51
     */
    public function goodsSpePriceAdd($data,$goods_id,$arr,$uniacid){

        $this->dataUpdate(['goods_id'=>$goods_id],['status'=>-1]);

        if(!empty($data)){

            foreach ($data as $v){

                if (strlen($v['price'])>8||strlen($v['original_price'])>8){

                    return ['code'=>'400','msg'=> '价格最多8位'];
                }

                $pid    = explode(',',$v['id']);

                $spe_id = [];

                if(empty($arr)){

                    return ['code'=>'400','msg'=> '规格不正确,请删除错误规格，重新编辑'];

                }
                foreach ($pid as $value){

                    if(!key_exists($value,$arr)){

                        return ['code'=>'400','msg'=> '规格不正确,请删除错误规格，重新编辑'];
                    }

                    $spe_id[] = $arr[$value];
                }
                $spe_price_id   = implode('-',$spe_id);

                $ins['uniacid'] = $uniacid;

                $ins['goods_id']= $goods_id;

                $ins['spe_id']= $spe_price_id;

                $ins['price']   = $v['price'];

                $ins['stock']   = $v['stock'];

                $ins['member_price'] = $v['member_price'];

                $ins['weight'] = $v['weight'];

                $ins['status']  = 1;
                //原价
                $ins['original_price'] = !empty($v['original_price'])?$v['original_price']:0;
                //成本价
                $ins['cost_price'] = !empty($v['cost_price'])?$v['cost_price']:0;

                $spe_price = $this->dataInfo(['goods_id'=>$goods_id,'spe_id'=>$spe_price_id,'uniacid'=>$uniacid]);

                if(empty($spe_price)) {

                    $res = $this->dataAdd($ins);

                }else{

                    $res = $this->dataUpdate(['id'=>$spe_price['id']],$ins);
                }
            }
        }

//        dump($data,$goods_id,$arr);exit;
        return !empty($res)?$res:1;
    }


    /**
     * @param $dis
     * @param int $page
     * @return mixed
     * 获取商品多规格价格库存
     */
    public function goodsSpePrice($dis){

        $data = $this->where($dis)->select()->toArray();

        return $data;
    }








}